﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUSQLDV;
using DTOQLDV;
using System.Runtime.InteropServices;
using System.Globalization;

namespace QLDV_DA
{
    public partial class frmNhapDuLieu : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapDuLieu(String fileName)
        {
            InitializeComponent();
            _fileName = fileName;
            _dsDung = new List<DTODuLieu>();           
            _dsSai = new List<DTODuLieuEX>();
            KhoiTao();           
        }
        private String _fileName;
        private List<DTODuLieu> _dsDung;
        private List<DTODuLieuEX> _dsSai;        
        private void KhoiTao()
        {
            //Load cac cot du lieu            
            rclMaDiaDiem.DisplayMember = "TenDiaDiem";
            rclMaDiaDiem.ValueMember = "MaDiaDiem";
           
            rclMaDichVu.DisplayMember = "TenDichVu";
            rclMaDichVu.ValueMember = "MaDichVu";
           
            rclMaDuong.DisplayMember = "TenDuong";
            rclMaDuong.ValueMember = "MaDuong";
                        
            rclMaPhuong.DisplayMember = "TenPhuong";
            rclMaPhuong.ValueMember = "MaPhuong";
           
            rclMaQuanHuyen.DisplayMember = "TenQuanHuyen";
            rclMaQuanHuyen.ValueMember = "MaQuanHuyen";
            
            rclMaTinhThanh.DisplayMember = "TenTinhThanh";
            rclMaTinhThanh.ValueMember = "MaTinhThanh";
           
            rclMaDichVu.DisplayMember = "TenDichVu";
            rclMaDichVu.ValueMember = "MaDichVu";

            rclMaDiaDiem.DataSource = SESSION._dsDiaDiem;
            rclMaDichVu.DataSource = SESSION._dsDichVu;
            rclMaDuong.DataSource = SESSION._dsDuong;
            rclMaPhuong.DataSource = SESSION._dsPhuong;
            rclMaQuanHuyen.DataSource = SESSION._dsQuanHuyen;
            rclMaTinhThanh.DataSource = SESSION._dsTinhThanh;
            rclMaDichVu.DataSource = SESSION._dsDichVu;

            rclTenDiaDiem.DataSource = SESSION._dsDiaDiem;
            rclTenDichVu.DataSource = SESSION._dsDichVu;
            rclTenDuong.DataSource = SESSION._dsDuong;
            rclTenPhuong.DataSource = SESSION._dsPhuong;
            rclTenQuanHuyen.DataSource = SESSION._dsQuanHuyen;
            rclTenTinhThanh.DataSource = SESSION._dsTinhThanh;
            rclTenDichVu.DataSource = SESSION._dsDichVu;
            thGridDanhSachDuLieuDung.DataSource = _dsDung;
            thGridDanhSachDuLieuSai.DataSource = _dsSai;
        }
        private void XuLiDuLieu()    
        {           
            BUSXuLi.MapDuLieu(_dsDung, _dsSai);
            LamTuoi();
        }        
        private void LoadDuLieu()
        {
            frmTienDo frm = new frmTienDo();
            try
            {
                _dsSai = BUSXuLi.DocDuLieuTuFileExcel(_fileName);              
                frm.ThucHienThread();            
                XuLiDuLieu();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Xảy ra lỗi " + ex.Message, "Thông báo");
            }
            finally
            {
                frm.Thoat();
                this.Activate();
            }
        }
        private void frmNhapDuLieu_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }    
        private void thGridviewDuLieuBiSai_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column == fclSTT)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }    

        private void thgridDanhSach_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column == clSTT)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

        private void cnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Gán dữ liệu vào datagridview
        /// </summary>
        private void LamTuoi()
        {     
            thGridDanhSachDuLieuDung.RefreshDataSource();
            thGridDanhSachDuLieuSai.RefreshDataSource();
        }
        /// <summary>
        ///Xử lí sự kiện nhấn nút chỉnh sữa thủ công 
        ///</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cnChinhSuaThuCong_Click(object sender, EventArgs e)
        {
            if (_dsSai.Count > 0)
            {
                frmTienDo frm = new frmTienDo();
                try
                {               
                    frm.ThucHienThread();
                    XuLiDuLieu();
                    LamTuoi();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    frm.Thoat();
                    this.Activate();
                }
            }
        }
        /// <summary>
        /// Hàm xử lí đồng ý fix dữ liêu =j tự động
        /// </summary>
        /// <param name="arr"></param>
        private void ThucHien(bool[] arr)
        {
            if (_dsSai.Count > 0)
            {
                frmTienDo frm = new frmTienDo();
                try
                {                 
                    frm.ThucHienThread();                   
                    BUSXuLi.CaiDatDuLieuNhap(_dsSai, arr);
                    XuLiDuLieu();
                    LamTuoi();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Xảy ra lỗi " + ex.Message, "Thông báo");
                }
                finally
                {
                    frm.Thoat();
                    this.Activate();
                }
            }
            else
            {
                MessageBox.Show("Không còn dòng nào sai", "Thông báo");
            }
        }
        /// <summary>
        /// Hàm xử lí bỏ qua các dòng xử lí sự kiện
        /// </summary>
        private void BoQua()
        {
            if (_dsSai.Count > 0)
            {
                _dsSai.Clear();
                LamTuoi();
            }
            else
            {
                MessageBox.Show("Không có dòng nào sai","Thống báo");
            }
        }
        private void cnDongY_Click(object sender, EventArgs e)
        {
            frmTienDo frm = new frmTienDo();
            try
            {              
                frm.ThucHienThread();             
                BUSDuLieu.ThemDanhSachDuLieu(_dsDung);                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Xảy ra lỗi " + ex.Message, "Thông báo");
            }
            finally
            {
                frm.Thoat();
                this.Activate();
                this.Close();
            }
        }
        private void cnChinhSuaTuDong_Click(object sender, EventArgs e)
        {
            frmXuLiTuDongDuLuLieuSai frm = new frmXuLiTuDongDuLuLieuSai();
            frm._thucHien = new frmXuLiTuDongDuLuLieuSai.ThucHien(ThucHien);
            frm._boQua = new frmXuLiTuDongDuLuLieuSai.BoQua(BoQua);
            frm.ShowDialog();         
        }        

        private void cnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show("Quá đễ xử lí sau");
            }
            catch (Exception ex) 
            {
            }
        }
    }
}